//
//  LoginController.m
//  HappyBaby
//
//  Created by lanou on 15/11/30.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "LoginController.h"
#import "LoginView.h"
#import "Define.h"
#import "RegisterController.h"
#import "SimpleRegistController.h"
#import <BmobSDK/Bmob.h>
#import "UMSocial.h"
@interface LoginController ()

@property (nonatomic, strong) LoginView *loginView;

@end

@implementation LoginController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"欢迎登录";
    self.navigationController.navigationBar.translucent = NO;
    
    
    self.loginView = [[LoginView alloc] initWithFrame:self.view.bounds];
    _loginView.backgroundColor = [UIColor whiteColor];
    self.view = _loginView;
    
    // 点击登录button执行方法
    [self.loginView.loginBtn addTarget:self action:@selector(loginBtn) forControlEvents:UIControlEventTouchUpInside];
    
    // 给注册button添加方法
    [self.loginView.registBtn addTarget:self action:@selector(registBtn) forControlEvents:UIControlEventTouchUpInside];
    // 给忘记密码button添加方法
    [self.loginView.forgetPasswordBtn addTarget:self action:@selector(forgetPasswordBtn) forControlEvents:UIControlEventTouchUpInside];

    // 第三方qq登录添加方法
    [self.loginView.qqBtn addTarget:self action:@selector(qqBtnLogin) forControlEvents:UIControlEventTouchUpInside];
    // 第三方qq登录添加方法
    [self.loginView.sinaWeiboBtn addTarget:self action:@selector(sinaBtnLogin) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark --点击登录按钮执行的方法
- (void) loginBtn
{
    // 用户登录
    [BmobUser loginWithUsernameInBackground:self.loginView.phoneOrEmail.text password:self.loginView.password.text block:^(BmobUser *user, NSError *error) {        
        if (user) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                // 登录成成功点击确定 执行的逻辑
                NSLog(@"用户信息");
            }]];
            
            [self presentViewController:alert animated:YES completion:nil];
        } else if (error) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录失败" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
    
    
}

#pragma mark --点击第三方qq登录的方法
- (void)qqBtnLogin
{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            // 配置自己qqurlScheme
        }});
    
}

#pragma mark --点击新浪button进行登录的方法
- (void)sinaBtnLogin
{
    //App Key：2543730482
    //App Secret：ea750e65bb762d9c79159bf1c4daadd7
    UMSocialSnsPlatform *snsPlatForm = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    snsPlatForm.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        // 获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
        }});
    
    
}

#pragma mark --点击注册button方法
- (void)registBtn
{
    RegisterController *registerVc = [[RegisterController alloc] init];
    [self.navigationController pushViewController:registerVc animated:YES];
}
#pragma mark --点击忘记密码按钮跳转方法
- (void)forgetPasswordBtn
{
    // 这个地方时复用快速注册界面找回密码
    SimpleRegistController *simpleRegistVc = [[SimpleRegistController alloc] init];
    simpleRegistVc.forgetPasswordFlag = self.loginView.forgetPasswordBtn.titleLabel.text;
    [self.navigationController pushViewController:simpleRegistVc animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
